package com.example.administrator.coldlain.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.administrator.coldlain.model.Vehicle;

/**
 * Created by Administrator on 2018/5/9 0009.
 */

public class VehicleDBHelper extends SQLiteOpenHelper {
    SQLiteDatabase db;
    private static final String CREATE_VEHICLETBL="create table vehicletable" +
            "(_id integer primary key autoincrement," +
            "plateNumber text," +
            "szvin text," +
            "sdate text," +
            "vehicleowner text)";
    private static final String CREATE_USERTABLE="create table usertable" +
            "(_id integer primary key autoincrement," +
            "username text," +
            "password text," +
            "ssex text," +
            "marrage text," +
            "favorite text," +
            "position text)";

    //通过构造方法实现数据库创建
    //构造方法什么时候调用,利用类创建对象的时候，对象实例化
    public VehicleDBHelper(Context context) {
        super(context, "vehicle.db", null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        this.db=sqLiteDatabase;
        db.execSQL(CREATE_VEHICLETBL);
        db.execSQL(CREATE_USERTABLE);
    }
    public int insertVehicle(ContentValues cv){
        int i=0;
        db=getWritableDatabase();
        i=(int) db.insert("vehicletable", null, cv);
        return i;
    }
    public int insertVehicle(Vehicle vehicle){
        int i=0;
        db=getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("plateNumber",vehicle.getPlateNumber());
        cv.put("szvin",vehicle.getSzvin());
        cv.put("sdate",vehicle.getSdate());
        cv.put("vehicleowner",vehicle.getVehicleowner());
        i=(int) db.insert("vehicletable", null, cv);
        return i;
    }

    public int deleteVehicle(){
        int i=0;
        db=getWritableDatabase();
        i=db.delete("vehicletable","",null);
        return i;
    }
    public int deleteVehicle(String _id){
        int i=0;
        db=getWritableDatabase();
        i=db.delete("vehicletable","_id=?",new String[]{_id});
        return i;
    }
    public int updateVehicle(String plateNumber,ContentValues cv){
        int i=0;
        db=getWritableDatabase();
        i=db.update("vehicletable",cv,"plateNumber=?",new String[]{plateNumber});
        return i;
    }
    public Cursor queryVehicle(){
        Cursor cursor=null;
        db=getReadableDatabase();
        cursor=db.query("vehicletable",null,null,null,null,null,null);
        return  cursor;
    }
    public int insertuser(ContentValues contentValues){
        int i=0;
        db=getWritableDatabase();
        i=(int) db.insert("usertable", null, contentValues);
        return i;
    }

    public Cursor userLogin(String username,String pwd){
        Cursor cursor=null;
        db=getReadableDatabase();
        cursor=db.query("usertable",null,"username=? and  password=?",new String[]{username,pwd},null,null,null);
        return  cursor;
    }

    public Cursor getUser(){
        Cursor cursor=null;
        db=getReadableDatabase();
        cursor=db.query("usertable",null,null,null,null,null,null);
        return  cursor;
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        this.db=sqLiteDatabase;
        db.execSQL(CREATE_VEHICLETBL);
        db.execSQL(CREATE_USERTABLE);
    }
}
